

'''父类'''
import time

import allure
from selenium import webdriver


class BasePage():

    _BASE_URL = ""

    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(3)
            self.driver.maximize_window()

        if not self.driver.current_url.startswith("http"):
            self.driver.get(self._BASE_URL)


    def do_find(self, by, locator=None):
        '''获取单个元素'''
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, locator=None):
        '''获取多个元素'''
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def do_send_keys(self, value, by, locator=None):
        ele = self.do_find(by, locator)
        ele.clear()
        ele.send_keys(value)

    def do_quit(self):
        self.driver.quit()

    def get_screen(self):
        timestamp = int(time.time())
    #     注意：一定要提前创建好images路径
        image_Path = f"./images/image_{timestamp}.PNG"
        # 截图
        self.driver.save_screenshot(image_Path)
    #     将截图放到报告的数据中
        allure.attach.file(image_Path, name="picture", attachment_type=allure.attachment_type.PNG)



